package com.iterator.controller.web.marketManager;

import com.iterator.bean.web.lc.CategoryBean.CategoryCreateBO;
import com.iterator.bean.web.lc.CategoryBean.CategoryCreateVO;
import com.iterator.bean.web.lc.CategoryBean.CategoryL1Vo;
import com.iterator.bean.web.lc.CategoryBean.CategoryListVO;
import com.iterator.bean.web.lc.CategoryBean.CategoryUpdateBO;
import com.iterator.bean.web.pojo.BaseRespVo;
import com.iterator.service.web.marketManager.CategoryServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("admin/category")
@RestController
public class CategoryController {
    @Autowired
    CategoryServiceImpl categoryService;


    @RequestMapping("list")
    @RequiresPermissions("admin:category:list")
    public BaseRespVo categoryList(){
        List<CategoryListVO> categoryVO = categoryService.getList();
        return BaseRespVo.ok(categoryVO);
    }

    /**
     * 获取一级类目
     * @return
     */
    @RequestMapping("l1")
    public BaseRespVo categoryL1(){
        List<CategoryListVO> categoryVO = categoryService.getL1();
        List<CategoryL1Vo> categoryL1Vos=new ArrayList<>();
        for (CategoryListVO categoryListVO : categoryVO) {
            categoryL1Vos.add(new CategoryL1Vo(categoryListVO.getName(),categoryListVO.getId()));
        }
        return BaseRespVo.ok(categoryL1Vos);
    }
    @RequestMapping("create")
    @RequiresPermissions("admin:category:create")
    public BaseRespVo create(@RequestBody @Valid CategoryCreateBO createBO, BindingResult result){
        if(result.hasFieldErrors()){
            return BaseRespVo.fail("标题不能为空");
        }
        CategoryCreateVO createVO = categoryService.create(createBO);
        return BaseRespVo.ok(createVO);
    }

    @RequestMapping("update")
    @RequiresPermissions("admin:category:update")
    public BaseRespVo update(@RequestBody @Valid CategoryUpdateBO updateBO, BindingResult result){
        if(result.hasFieldErrors()){
            return BaseRespVo.fail("标题不能为空");
        }
        Integer update = categoryService.update(updateBO);
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    @RequiresPermissions("admin:category:delete")
    public BaseRespVo delete(@RequestBody  CategoryUpdateBO updateBO){

        Integer update = categoryService.delete(updateBO);
        return BaseRespVo.ok();
    }


}
